#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
主题: 手动遍历迭代器
问题: 你想遍历一个可迭代对象中的所有元素，但是却不想使用for循环。
提示 : 
"""

def recipe1():
    manual_iter()
    manual_iter_ext()

def recipe2():
    """使用 for 循环语句用来遍历一个可迭代对象"""
    items = [1, 2, 3]
    it = iter(items)
    print(f"{next(it) = }")
    print(f"{next(it) = }")
    print(f"{next(it) = }")
    print(f"{next(it) = }")

def manual_iter():
    """手动的遍历可迭代对象"""
    with open('../somefile.txt') as f:
        try:
            while True:
                line = next(f)
                print(line, end='')
        except StopIteration:
            print('\nerror')

def manual_iter_ext():
    """手动的遍历可迭代对象"""
    with open('../somefile.txt') as f:
        while True:
            line = next(f, None)
            if line is None:
                break
            print(line, end='')

def main():
    print('recipe1'.center(20, '*'))
    recipe1() 
    print('recipe2'.center(20, '*'))
    recipe2() 

if __name__ == '__main__':
    main()             